<?php

class JobController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->_helper->redirector('list', 'job', '');
    }

    public function listAction() {

        $errorMessage = Array();
        $successmessage = "";
        $where = "";
        $searchtype = "";
        $searchtext = "";
        $searchunit = "";
        $searchstatus = "";
        $orderField = 'job_id';
        $sort = 'asc';

        /* Pagination Login */
        $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
        $currentPageReviews = 1;

        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }


        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

        if ($this->_request->isPost()) {
            $postedData = $this->_request->getPost();

            //Multiple Delete Logic 
            if (isset($postedData["multiaction"]) && ($postedData["multiaction"] != "")) {
                $action = trim($postedData["multiaction"]);
                if (isset($postedData["multicheck"]) && count($postedData["multicheck"]) > 0) {
                    $ids = implode(",", $_POST["multicheck"]);
                    $result = $this->deleteAction($_POST["multicheck"]);
                } else {
                    $errorMessage[] = "Please select atleast one checkbox";
                }
            }

            /* Search Facility Over Here */
            if (isset($postedData["searchtype"]) && ($postedData["searchtype"] != "")) {
                $searchtype = $postedData["searchtype"];
                if ($searchtype == "job_code") {
                    $searchtext = $postedData["searchtext"];
                    if ($searchtext == '') {
                        $errorMessage[] = "Please enter search text for search.";
                    } else {
                        $where = "$searchtype LIKE '%$searchtext%'";
                    }
                } else if ($searchtype == "job_uom") {
                    $searchunit = $postedData["searchunit"];
                    if ($searchunit == '') {
                        $errorMessage[] = "Please select unit type for search.";
                    } else {
                        $where = "`job_uom` = '" . $searchunit . "' ";
                    }
                } else if ($searchtype == "job_status") {
                    $searchstatus = $postedData["searchstatus"];
                    if ($searchstatus == '') {
                        $errorMessage[] = "Please select status type for search.";
                    } else {
                        $where = "`job_status` = '" . $searchstatus . "' ";
                    }
                }
            }
        }

        $jobModel = new Model_Job();
        $jobsData = $jobModel->fetchJobs($where, $itemsPerPageReviews, $offset, $orderField, $sort);

        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal($jobModel->getJobCount($where));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'job', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        $this->view->paginator = $paginator;
        $this->view->jobsData = $jobsData;

        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;

        $this->view->unitArray = $jobModel->getUnitArray();
        $this->view->statusArray = $jobModel->getStatusArray();
        $this->view->searchtype = $searchtype;
        $this->view->searchtext = $searchtext;
        $this->view->searchunit = $searchunit;
        $this->view->searchstatus = $searchstatus;


        $indexUrl = $this->view->url(array('controller' => 'index', 'action' => 'index'), null, TRUE);
        $jobUrl = $this->view->url(array('controller' => 'job', 'action' => 'list'), null, TRUE);
        $this->view->breadcrumbs = array($indexUrl => "Home", $jobUrl => "Job", "" => "Job Listing");
    }

    public function deleteAction($ids = Array()) {
        //Load Job Model 
        $jobModel = new Model_Job();

        if ($this->_getParam('job_id') && $this->_getParam('job_id') != "") {
            $job_id = $this->_getParam('job_id');
            $jobModel->deleteJob($job_id);

            $this->addSuccessMessage("Jobs deleted succesfully");
            $this->_helper->redirector('list', 'job', '');
        } else {
            $jobModel->deleteJob($ids);
            $this->addSuccessMessage("Jobs deleted succesfully");
            $this->_helper->redirector('list', 'job', '');
        }
    }

    public function formAction() {

        //Initialize
        $successmessage = "";
        $errorMessageArray = Array();
        $data = array();
        $jobData = Array();
        $this->view->mode = "Add";

        //Load Job Model
        $jobModel = new Model_Job();

        //Load Job Form
        $jobForm = new Form_JobForm();

        //Load the status array for the select box in form 
        $statusArray = $jobModel->getStatusArray();
        $jobForm->job_status->addMultiOptions($statusArray);

        //Load the unit array for the select box in form 
        $unitArray = $jobModel->getUnitArray();
        $jobForm->job_uom->addMultiOptions($unitArray);

        //Check for the job Parameter
        $jobData['job_id'] = $this->_request->getParam('job_id');
        $job_id = (!empty($jobData['job_id'])) ? $jobData['job_id'] : null;

        if (!empty($job_id)) {
            //Set the Mode to Edit
            $this->view->mode = "Edit";
            //Fetch data to be loaded in form 
            $data = $jobModel->fetchEntryById($job_id);
        }

        //Populate form 
        $this->view->form = $jobForm->populate($data);

        if ($this->getRequest()->isPost()) {

            //Check for Errors
            $postedData = $this->_request->getPost();
            if (!$jobForm->isValid($postedData)) {
                $errorMessage = $jobForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {
                if (count($errorMessageArray) > 0) {
                    
                } else {
                    //Job save
                    $jobData['job_id'] = $job_id;
                    $jobData['job_code'] = $postedData['job_code'];
                    $jobData['job_description'] = $postedData['job_description'];
                    $jobData['job_uom'] = $postedData['job_uom'];
                    $jobData['job_materialcost'] = GTL_Common::setNumberFormat($postedData['job_materialcost']);
                    $jobData['job_materialsrp'] = GTL_Common::setNumberFormat($postedData['job_materialsrp']);
                    if ($this->view->mode == "Add") {
                        $jobData['job_created'] = date("Y-m-d H:i:s");
                    }
                    $jobData['job_modified'] = date("Y-m-d H:i:s");
                    $jobData['job_status'] = $postedData['job_status'];
                    $jobModel->save($jobData, 'job_id');
                    if ($this->view->mode == "Add") {
                        $successmessage = "Job added successfully";
                    } else {
                        $successmessage = "Job updated successfully";
                    }
                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('list', 'job', '');
                }
            }
        }

        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $jobForm;
        $indexUrl = $this->view->url(array('controller' => 'index', 'action' => 'index'), null, TRUE);
        $jobUrl = $this->view->url(array('controller' => 'job', 'action' => 'list'), null, TRUE);
        $this->view->breadcrumbs = array($indexUrl => "Home", $jobUrl => "Job", "" => $this->view->mode . " Job");
    }

}

?>
